<?php
namespace Admin\Controller;
use Tools\AdminController;
class RoleController extends AdminController {
    function updaterole($id,$role){
    	$roleid=D('role')->field('id')->where("name='$role'")->find();
		//echo $roleid['id'];
    	$manager=new \Model\ManagerModel();
		$sql="update manager set rolename='".$role."',roleid=".$roleid['id']." where id=$id;";
		//echo $sql;
		if($manager->execute($sql)){
			echo $roleid['id'];
		}else{
			echo "失败";
		}
    }
	function showlist(){
		$role=new \Model\RoleModel();
		//分页相关
		$total=$role->count();
		$per=2;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$sql="select * from role order by id desc ".$page_obj->limit;
		$roles=$role->query($sql);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('total',$total);
		$this->assign('pagelist',$pagelist);
		$this->assign('roles',$roles);
		//var_dump($roles);
		$this->display();
	}
	function assignauth($id){
		if(!empty($_POST)){
			//var_dump($_POST);
			//把$auth_id由数组变为用,链接的String字符串
			$authids=implode(',',$_POST['authids']);
			$sql="update role set authids='".$authids."' where id={$_POST['roleid']};";
			//echo $sql;
			$rolemodel=new \Model\RoleModel();
			if($rolemodel->execute($sql)){
			$this->success('Index/success');
			$this->redirect('Role/showlist',array(),3," ");
			}else{
			$this->error('Index/error');
			$this->redirect('Role/showlist',array(),3," ");
			}
//			$this->success('Index/success');
//			$this->redirect('Manager/showlist',array(),3," ");
		}else{
			//查找该角色已有权限
			$role_info=D('Role')->find($id);
			//获得已经拥有的权限信息，并变为Array数组
			$had_auth=explode(',',$role_info['authids']);
			//查找全部权限
			$auth_infoA=D('Auth')->where("level=0")->select();
			$auth_infoB=D('Auth')->where("level=1")->select();
			//var_dump($had_auth);
			$this->assign('had_auth',$had_auth);
			$this->assign('auth_infoA',$auth_infoA);
			$this->assign('auth_infoB',$auth_infoB);
			$this->assign("role_info",$role_info);
			$this->display();
		}
			
			
	}
	function addrole(){
		//var_dump($_POST);
		if(D('Role')->add($_POST)){
			$this->success('Index/success');
			$this->redirect('Role/showlist',array(),3," ");
		}else{
			$this->error('Index/error');
			$this->redirect('Role/showlist',array(),3," ");
		}	
		
	}
	function deleterole($id){
		if(D('Role')->delete($id)){
			$this->success('Index/success');
			$this->redirect('Role/showlist',array(),3," ");
		}else{
			$this->error('Index/error');
			$this->redirect('Role/showlist',array(),3," ");
		}
	}
}